package com.ly.datacenter.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.ly.datacenter.component.pager.pojo.Pager;
import com.ly.datacenter.crud.json.ReadJSON;
import com.ly.datacenter.database.definition.HttpParameter4DatabaseCRUD;
import com.ly.datacenter.http.util.HttpServletUtil;
import com.ly.datacenter.jdbc.util.DAOUtil;

/**
 * function:
 * @author watson  email: watson.wu@hotmail.com
 * @date 2009-9-3
 */
@SuppressWarnings("unchecked")
public class TableOperaterServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = request.getParameter(HttpParameter4DatabaseCRUD.X_TYPE);
		String table = request.getParameter(HttpParameter4DatabaseCRUD.X_TABLE);
		HttpServletUtil.configResponseJSON(request, response);
		if(StringUtils.isBlank(type) || StringUtils.isBlank(table)) return;
		if("create".equals(type)) {
			Map paramters = HttpServletUtil.getParameters(request);
			boolean flag = DAOUtil.insert(table, paramters);
			response.getWriter().print("\"flag\":"+flag);
		} else if("read".equals(type)) {
			Map conditions = HttpServletUtil.getParameters(request);
//			conditions = request.getParameterMap();
			Map pagerParamters = getPagerParameter(request);
//			conditions = removePagerParmeter(conditions, pagerParamters);
//			conditions.remove("table");
//			conditions.remove("type");

			Pager pager = DAOUtil.queryPager(table, conditions, pagerParamters);
			response.getWriter().print(new ReadJSON(pager).toString());
		} else if("update".equals(type)) {
			Map paramters = HttpServletUtil.getParameters(request);
			boolean flag = DAOUtil.updateByPK(table, paramters);
			response.getWriter().print("\"flag\":"+flag);
		} else if("delete".equals(type)) {
			String ids = request.getParameter("ids");
			String[] pks = ids.split(",");
			boolean flag = DAOUtil.deleteByPk(table, pks);
			response.getWriter().print("\"flag\":"+flag);
		}
	}

	@SuppressWarnings("unused")
	private Map getPagerParameter(Map conditions) {
		Map map = new HashMap();
		Object pageObj = conditions.get(Pager.PAGE);
		String page = "1";
		if(null != pageObj)
			if(pageObj instanceof String[])
				page = ((String[])pageObj)[0];
			else
				page = pageObj.toString();
		map.put("page", page);
		return map;
	}

	private Map getPagerParameter(HttpServletRequest request) {
		HashMap map = new HashMap();
		String page = request.getParameter(Pager.PAGE);
		if(page != null)
			map.put("page", page);
		String pagesize = request.getParameter("rows");
		if(pagesize != null)
			map.put("pagesize", pagesize);
		String sord = request.getParameter("sord");
		if(null != sord)
			map.put("sort", sord);
		return map;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}